{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import keras\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Our examples of exclusive OR.\n",
    "# x_train is sample data\n",
    "# y_train the expected outcome for example\n",
    "x_train = np.array([[0, 0],\n",
    "                    [0, 1],\n",
    "                    [1, 0],\n",
    "                    [1, 1]])\n",
    "y_train = np.array([[0],\n",
    "                    [1],\n",
    "                    [1],\n",
    "                    [0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# The base Keras model class\n",
    "\n",
    "from keras.models import Sequential\n",
    "\n",
    "# The basic layer of the network\n",
    "# Dense is a fully-connected set of neurons\n",
    "\n",
    "from keras.layers import Dense, Activation\n",
    "\n",
    "# Get stochastic gradient descent, though there are others\n",
    "from keras.optimizers import SGD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "dense_22 (Dense)             (None, 10)                30        \n",
      "_________________________________________________________________\n",
      "activation_10 (Activation)   (None, 10)                0         \n",
      "_________________________________________________________________\n",
      "dense_23 (Dense)             (None, 1)                 11        \n",
      "_________________________________________________________________\n",
      "activation_11 (Activation)   (None, 1)                 0         \n",
      "=================================================================\n",
      "Total params: 41.0\n",
      "Trainable params: 41.0\n",
      "Non-trainable params: 0.0\n",
      "_________________________________________________________________\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "model = Sequential()\n",
    "\n",
    "# Add a fully connected hidden layer with 10 neurons\n",
    "# The input shape is the shape of an individual sample vector\n",
    "# This is only necessary in the first layer, any additional\n",
    "# layers will calculate the shape automatically by the definition\n",
    "# of the model up to that point\n",
    "\n",
    "num_neurons = 10\n",
    "model.add(Dense(num_neurons, input_dim=2))\n",
    "model.add(Activation('tanh'))\n",
    "\n",
    "# The output layer one neuron to output 0 or 1 \n",
    "model.add(Dense(1))\n",
    "model.add(Activation('sigmoid'))\n",
    "print(model.summary())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.5       ]\n",
      " [ 0.43494844]\n",
      " [ 0.50295198]\n",
      " [ 0.42517585]]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict(x_train))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sgd = SGD(lr=0.1)\n",
    "model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/100\n",
      "4/4 [==============================] - 0s - loss: 0.6917 - acc: 0.7500\n",
      "Epoch 2/100\n",
      "4/4 [==============================] - 0s - loss: 0.6911 - acc: 0.5000\n",
      "Epoch 3/100\n",
      "4/4 [==============================] - 0s - loss: 0.6906 - acc: 0.5000\n",
      "Epoch 4/100\n",
      "4/4 [==============================] - 0s - loss: 0.6901 - acc: 0.5000\n",
      "Epoch 5/100\n",
      "4/4 [==============================] - 0s - loss: 0.6897 - acc: 0.5000\n",
      "Epoch 6/100\n",
      "4/4 [==============================] - 0s - loss: 0.6893 - acc: 0.5000\n",
      "Epoch 7/100\n",
      "4/4 [==============================] - 0s - loss: 0.6890 - acc: 0.5000\n",
      "Epoch 8/100\n",
      "4/4 [==============================] - 0s - loss: 0.6887 - acc: 0.5000\n",
      "Epoch 9/100\n",
      "4/4 [==============================] - 0s - loss: 0.6883 - acc: 0.5000\n",
      "Epoch 10/100\n",
      "4/4 [==============================] - 0s - loss: 0.6881 - acc: 0.5000\n",
      "Epoch 11/100\n",
      "4/4 [==============================] - 0s - loss: 0.6878 - acc: 0.5000\n",
      "Epoch 12/100\n",
      "4/4 [==============================] - 0s - loss: 0.6875 - acc: 0.5000\n",
      "Epoch 13/100\n",
      "4/4 [==============================] - 0s - loss: 0.6873 - acc: 0.5000\n",
      "Epoch 14/100\n",
      "4/4 [==============================] - 0s - loss: 0.6871 - acc: 0.5000\n",
      "Epoch 15/100\n",
      "4/4 [==============================] - 0s - loss: 0.6868 - acc: 0.5000\n",
      "Epoch 16/100\n",
      "4/4 [==============================] - 0s - loss: 0.6866 - acc: 0.5000\n",
      "Epoch 17/100\n",
      "4/4 [==============================] - 0s - loss: 0.6864 - acc: 0.5000\n",
      "Epoch 18/100\n",
      "4/4 [==============================] - 0s - loss: 0.6862 - acc: 0.5000\n",
      "Epoch 19/100\n",
      "4/4 [==============================] - 0s - loss: 0.6860 - acc: 0.5000\n",
      "Epoch 20/100\n",
      "4/4 [==============================] - 0s - loss: 0.6858 - acc: 0.5000\n",
      "Epoch 21/100\n",
      "4/4 [==============================] - 0s - loss: 0.6856 - acc: 0.5000\n",
      "Epoch 22/100\n",
      "4/4 [==============================] - 0s - loss: 0.6854 - acc: 0.5000\n",
      "Epoch 23/100\n",
      "4/4 [==============================] - 0s - loss: 0.6852 - acc: 0.5000\n",
      "Epoch 24/100\n",
      "4/4 [==============================] - 0s - loss: 0.6851 - acc: 0.5000\n",
      "Epoch 25/100\n",
      "4/4 [==============================] - 0s - loss: 0.6849 - acc: 0.5000\n",
      "Epoch 26/100\n",
      "4/4 [==============================] - 0s - loss: 0.6847 - acc: 0.5000\n",
      "Epoch 27/100\n",
      "4/4 [==============================] - 0s - loss: 0.6845 - acc: 0.5000\n",
      "Epoch 28/100\n",
      "4/4 [==============================] - 0s - loss: 0.6843 - acc: 0.5000\n",
      "Epoch 29/100\n",
      "4/4 [==============================] - 0s - loss: 0.6841 - acc: 0.5000\n",
      "Epoch 30/100\n",
      "4/4 [==============================] - 0s - loss: 0.6840 - acc: 0.5000\n",
      "Epoch 31/100\n",
      "4/4 [==============================] - 0s - loss: 0.6838 - acc: 0.5000\n",
      "Epoch 32/100\n",
      "4/4 [==============================] - 0s - loss: 0.6836 - acc: 0.5000\n",
      "Epoch 33/100\n",
      "4/4 [==============================] - 0s - loss: 0.6834 - acc: 0.5000\n",
      "Epoch 34/100\n",
      "4/4 [==============================] - 0s - loss: 0.6832 - acc: 0.5000\n",
      "Epoch 35/100\n",
      "4/4 [==============================] - 0s - loss: 0.6830 - acc: 0.5000\n",
      "Epoch 36/100\n",
      "4/4 [==============================] - 0s - loss: 0.6828 - acc: 0.5000\n",
      "Epoch 37/100\n",
      "4/4 [==============================] - 0s - loss: 0.6827 - acc: 0.5000\n",
      "Epoch 38/100\n",
      "4/4 [==============================] - 0s - loss: 0.6825 - acc: 0.5000\n",
      "Epoch 39/100\n",
      "4/4 [==============================] - 0s - loss: 0.6823 - acc: 0.5000\n",
      "Epoch 40/100\n",
      "4/4 [==============================] - 0s - loss: 0.6821 - acc: 0.5000\n",
      "Epoch 41/100\n",
      "4/4 [==============================] - 0s - loss: 0.6819 - acc: 0.5000\n",
      "Epoch 42/100\n",
      "4/4 [==============================] - 0s - loss: 0.6817 - acc: 0.5000\n",
      "Epoch 43/100\n",
      "4/4 [==============================] - 0s - loss: 0.6815 - acc: 0.5000\n",
      "Epoch 44/100\n",
      "4/4 [==============================] - 0s - loss: 0.6813 - acc: 0.5000\n",
      "Epoch 45/100\n",
      "4/4 [==============================] - 0s - loss: 0.6811 - acc: 0.5000\n",
      "Epoch 46/100\n",
      "4/4 [==============================] - 0s - loss: 0.6809 - acc: 0.5000\n",
      "Epoch 47/100\n",
      "4/4 [==============================] - 0s - loss: 0.6807 - acc: 0.5000\n",
      "Epoch 48/100\n",
      "4/4 [==============================] - 0s - loss: 0.6805 - acc: 0.5000\n",
      "Epoch 49/100\n",
      "4/4 [==============================] - 0s - loss: 0.6803 - acc: 0.5000\n",
      "Epoch 50/100\n",
      "4/4 [==============================] - 0s - loss: 0.6801 - acc: 0.5000\n",
      "Epoch 51/100\n",
      "4/4 [==============================] - 0s - loss: 0.6799 - acc: 0.5000\n",
      "Epoch 52/100\n",
      "4/4 [==============================] - 0s - loss: 0.6796 - acc: 0.5000\n",
      "Epoch 53/100\n",
      "4/4 [==============================] - 0s - loss: 0.6794 - acc: 0.5000\n",
      "Epoch 54/100\n",
      "4/4 [==============================] - 0s - loss: 0.6792 - acc: 0.5000\n",
      "Epoch 55/100\n",
      "4/4 [==============================] - 0s - loss: 0.6790 - acc: 0.5000\n",
      "Epoch 56/100\n",
      "4/4 [==============================] - 0s - loss: 0.6788 - acc: 0.5000\n",
      "Epoch 57/100\n",
      "4/4 [==============================] - 0s - loss: 0.6785 - acc: 0.5000\n",
      "Epoch 58/100\n",
      "4/4 [==============================] - 0s - loss: 0.6783 - acc: 0.5000\n",
      "Epoch 59/100\n",
      "4/4 [==============================] - 0s - loss: 0.6781 - acc: 0.5000\n",
      "Epoch 60/100\n",
      "4/4 [==============================] - 0s - loss: 0.6779 - acc: 0.5000\n",
      "Epoch 61/100\n",
      "4/4 [==============================] - 0s - loss: 0.6776 - acc: 0.5000\n",
      "Epoch 62/100\n",
      "4/4 [==============================] - 0s - loss: 0.6774 - acc: 0.7500\n",
      "Epoch 63/100\n",
      "4/4 [==============================] - 0s - loss: 0.6772 - acc: 0.7500\n",
      "Epoch 64/100\n",
      "4/4 [==============================] - 0s - loss: 0.6769 - acc: 1.0000\n",
      "Epoch 65/100\n",
      "4/4 [==============================] - 0s - loss: 0.6767 - acc: 1.0000\n",
      "Epoch 66/100\n",
      "4/4 [==============================] - 0s - loss: 0.6764 - acc: 1.0000\n",
      "Epoch 67/100\n",
      "4/4 [==============================] - 0s - loss: 0.6762 - acc: 1.0000\n",
      "Epoch 68/100\n",
      "4/4 [==============================] - 0s - loss: 0.6759 - acc: 1.0000\n",
      "Epoch 69/100\n",
      "4/4 [==============================] - 0s - loss: 0.6757 - acc: 1.0000\n",
      "Epoch 70/100\n",
      "4/4 [==============================] - 0s - loss: 0.6754 - acc: 1.0000\n",
      "Epoch 71/100\n",
      "4/4 [==============================] - 0s - loss: 0.6751 - acc: 1.0000\n",
      "Epoch 72/100\n",
      "4/4 [==============================] - 0s - loss: 0.6749 - acc: 1.0000\n",
      "Epoch 73/100\n",
      "4/4 [==============================] - 0s - loss: 0.6746 - acc: 1.0000\n",
      "Epoch 74/100\n",
      "4/4 [==============================] - 0s - loss: 0.6743 - acc: 1.0000\n",
      "Epoch 75/100\n",
      "4/4 [==============================] - 0s - loss: 0.6741 - acc: 1.0000\n",
      "Epoch 76/100\n",
      "4/4 [==============================] - 0s - loss: 0.6738 - acc: 1.0000\n",
      "Epoch 77/100\n",
      "4/4 [==============================] - 0s - loss: 0.6735 - acc: 1.0000\n",
      "Epoch 78/100\n",
      "4/4 [==============================] - 0s - loss: 0.6732 - acc: 1.0000\n",
      "Epoch 79/100\n",
      "4/4 [==============================] - 0s - loss: 0.6729 - acc: 1.0000\n",
      "Epoch 80/100\n",
      "4/4 [==============================] - 0s - loss: 0.6727 - acc: 1.0000\n",
      "Epoch 81/100\n",
      "4/4 [==============================] - 0s - loss: 0.6724 - acc: 1.0000\n",
      "Epoch 82/100\n",
      "4/4 [==============================] - 0s - loss: 0.6721 - acc: 1.0000\n",
      "Epoch 83/100\n",
      "4/4 [==============================] - 0s - loss: 0.6718 - acc: 1.0000\n",
      "Epoch 84/100\n",
      "4/4 [==============================] - 0s - loss: 0.6715 - acc: 1.0000\n",
      "Epoch 85/100\n",
      "4/4 [==============================] - 0s - loss: 0.6712 - acc: 1.0000\n",
      "Epoch 86/100\n",
      "4/4 [==============================] - 0s - loss: 0.6708 - acc: 1.0000\n",
      "Epoch 87/100\n",
      "4/4 [==============================] - 0s - loss: 0.6705 - acc: 1.0000\n",
      "Epoch 88/100\n",
      "4/4 [==============================] - 0s - loss: 0.6702 - acc: 1.0000\n",
      "Epoch 89/100\n",
      "4/4 [==============================] - 0s - loss: 0.6699 - acc: 1.0000\n",
      "Epoch 90/100\n",
      "4/4 [==============================] - 0s - loss: 0.6696 - acc: 1.0000\n",
      "Epoch 91/100\n",
      "4/4 [==============================] - 0s - loss: 0.6692 - acc: 1.0000\n",
      "Epoch 92/100\n",
      "4/4 [==============================] - 0s - loss: 0.6689 - acc: 1.0000\n",
      "Epoch 93/100\n",
      "4/4 [==============================] - 0s - loss: 0.6686 - acc: 1.0000\n",
      "Epoch 94/100\n",
      "4/4 [==============================] - 0s - loss: 0.6682 - acc: 1.0000\n",
      "Epoch 95/100\n",
      "4/4 [==============================] - 0s - loss: 0.6679 - acc: 1.0000\n",
      "Epoch 96/100\n",
      "4/4 [==============================] - 0s - loss: 0.6675 - acc: 1.0000\n",
      "Epoch 97/100\n",
      "4/4 [==============================] - 0s - loss: 0.6672 - acc: 1.0000\n",
      "Epoch 98/100\n",
      "4/4 [==============================] - 0s - loss: 0.6668 - acc: 1.0000\n",
      "Epoch 99/100\n",
      "4/4 [==============================] - 0s - loss: 0.6664 - acc: 1.0000\n",
      "Epoch 100/100\n",
      "4/4 [==============================] - 0s - loss: 0.6661 - acc: 1.0000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x1098f44a8>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Here is where we train the model\n",
    "model.fit(x_train, y_train,\n",
    "          epochs=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved.\n"
     ]
    }
   ],
   "source": [
    "# And then we save the structure and learned weights for later use\n",
    "model_structure = model.to_json()\n",
    "with open(\"basic_model.json\", \"w\") as json_file:\n",
    "    json_file.write(model_structure)\n",
    "\n",
    "model.save_weights(\"basic_weights.h5\")\n",
    "print('Model saved.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4/4 [==============================] - 0s\n",
      "[[0]\n",
      " [1]\n",
      " [1]\n",
      " [0]]\n",
      "[[ 0.0035659 ]\n",
      " [ 0.99123639]\n",
      " [ 0.99285167]\n",
      " [ 0.00907462]]\n"
     ]
    }
   ],
   "source": [
    "print(model.predict_classes(x_train))\n",
    "print(model.predict(x_train))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
